<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="联系人" prop="name">
        <el-input
            v-model="queryParams.name"
            placeholder="请输入联系人"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="phone">
        <el-input
            v-model="queryParams.phone"
            placeholder="请输入联系电话"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否联系" prop="isContact">
        <el-select v-model="queryParams.isContact" placeholder="请选择是否联系" clearable>
          <el-option
              v-for="dict in sys_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否确认" prop="isConfirm">
        <el-select v-model="queryParams.isConfirm" placeholder="请选择是否确认" clearable>
          <el-option
              v-for="dict in sys_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['system:shopContact:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['system:shopContact:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['system:shopContact:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['system:shopContact:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="shopContactList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="联系人" width="100" align="center" prop="name"/>
      <el-table-column label="联系电话" width="150" align="center" prop="phone"/>
      <el-table-column label="店铺名称" width="200" align="center" prop="shopName"/>
      <el-table-column label="店铺地址" width="200" align="center" prop="address"/>
      <el-table-column label="申请时间" width="180" align="center" prop="createTime"/>
      <el-table-column label="地区" width="100" align="center" prop="cityName"/>
      <el-table-column label="是否联系" width="100" align="center" prop="isContact">
        <template #default="scope">
          <dict-tag :options="sys_yes_no" :value="scope.row.isContact"/>
        </template>
      </el-table-column>
      <el-table-column label="是否确认" width="100" align="center" prop="isConfirm">
        <template #default="scope">
          <dict-tag :options="sys_yes_no" :value="scope.row.isConfirm"/>
        </template>
      </el-table-column>
      <el-table-column label="沟通结果" align="center" width="300" prop="communicateResult"/>
      <el-table-column label="操作" align="center" width="200" class-name="small-padding fixed-width" fixed="right">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['system:shopContact:edit']">修改
          </el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['system:shopContact:remove']">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改入驻联系对话框 -->
    <el-dialog :title="title" v-model="open" width="1000px" append-to-body>
      <el-form ref="shopContactRef" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="联系人" prop="name">
              <el-input v-model="form.name" placeholder="请输入联系人" maxlength="10"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="联系电话" prop="phone">
              <el-input v-model="form.phone" placeholder="请输入联系电话" maxlength="13"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="店铺名称" prop="shopName">
              <el-input v-model="form.shopName" type="textarea" placeholder="请输入内容" maxlength="20"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="店铺地址" prop="address">
              <el-input v-model="form.address" type="textarea" placeholder="请输入内容" maxlength="30"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="省市" prop="cityCode">
              <el-cascader-city v-model="form.cityCode" :is-save="useUserStore().userType === '0' ?true:false"></el-cascader-city>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="是否联系" prop="isContact">
              <el-select v-model="form.isContact" placeholder="请选择是否联系">
                <el-option
                    v-for="dict in sys_yes_no"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否确认" prop="isConfirm">
              <el-select v-model="form.isConfirm" placeholder="请选择是否确认">
                <el-option
                    v-for="dict in sys_yes_no"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="沟通结果" prop="communicateResult">
              <editor v-model="form.communicateResult" :min-height="192"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="ShopContact">
import {
  listShopContact,
  getShopContact,
  delShopContact,
  addShopContact,
  updateShopContact
} from "@/api/system/shopContact";
import {onBeforeUnmount, watch} from "vue";
import useUserStore from "@/store/modules/user";
const {proxy} = getCurrentInstance();
const {sys_yes_no} = proxy.useDict('sys_yes_no');

const shopContactList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: undefined,
    phone: undefined,
    shopName: undefined,
    address: undefined,
    isContact: undefined,
    isConfirm: undefined,
    communicateResult: undefined,
  },
  rules: {
    name:[
      {required: true, message: "联系人不能为空", trigger: "blur"}
    ],
    phone:[
      {required: true, message: "联系电话不能为空", trigger: "blur"},
      {
        pattern: /^(1[3|4|5|6|7|8|9]\d{9})$|^(\d{3,4}-\d{7,8})$/,
        message: '请输入正确的手机号码',
        trigger: 'blur'
      }
    ],
    shopName:[
      {required: true, message: "店铺名称不能为空", trigger: "blur"}
    ],
    address:[
      {required: true, message: "店铺地址不能为空", trigger: "blur"}
    ],
    isContact:[
      {required: true, message: "请选择是否联系", trigger: "blur"}
    ],
    isConfirm:[
      {required: true, message: "请选择是否确认", trigger: "blur"}
    ],
    cityCode:[
      {required: true, message: "请选择所在地", trigger: "blur"}
    ],
    communicateResult:[
      {required: true, message: "请输入沟通结果", trigger: "blur"}
    ]
  }
});

const {queryParams, form, rules} = toRefs(data);

/** 查询入驻联系列表 */
function getList() {
  loading.value = true;
  listShopContact(queryParams.value).then(response => {
    shopContactList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: undefined,
    name: undefined,
    phone: undefined,
    shopName: undefined,
    address: undefined,
    uid: undefined,
    isContact: undefined,
    isConfirm: undefined,
    communicateResult: undefined,
    createTime: undefined,
    updateTime: undefined
  };
  proxy.resetForm("shopContactRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加入驻联系";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getShopContact(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改入驻联系";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["shopContactRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateShopContact(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addShopContact(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除入该数据项？').then(function () {
    return delShopContact(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('system/shopContact/export', {
    ...queryParams.value
  }, `shopContact_${new Date().getTime()}.xlsx`)
}

getList();
const stopWatching = watch(() => useUserStore().cascaderKey, (v) => {
  getList();
}, { immediate: true });
onBeforeUnmount(() => {
  stopWatching();
});
</script>
